#include <comlib.h>
#include <stdio.h>
#include <mysql.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#define INSERT_Status "INSERT INTO tblStatus VALUES (?,?,1,1,now(),'192.168.0.1')"




int main()
{
	int nSock, nSize;
	char szBuf[256];
	struct sockaddr_in cliaddr;
	socklen_t len=sizeof(cliaddr);
	struct sockaddr *paddr = (struct sockaddr *)&cliaddr;

	int i;
	MYSQL *conn,*conn2;
	MYSQL_RES *result;
	MYSQL_ROW row;

	MYSQL_STMT    *stmt;
	MYSQL_BIND    bind[3];
	my_ulonglong  affected_rows;
	int           param_count;
	int           int_data,int_data2;
	my_bool       is_null;


	conn = mysql_init(NULL);
	conn2 = mysql_init(NULL);
	if(mysql_real_connect(conn,"localhost","root","mysql","secure",3306,NULL,0) == NULL)
	{
		fprintf(stderr,"sorry,no database connection...\n");
		return 1;
	}
	if(mysql_real_connect(conn2,"localhost","root","mysql","secure",3306,NULL,0) == NULL)
	{
		fprintf(stderr,"sorry,no database connection...\n");
		return 1;
	}
//stmt insert
	stmt = mysql_stmt_init(conn);
	if (!stmt)
	{
	  fprintf(stderr, " mysql_stmt_init(), out of memory\n");
	  exit(0);
	}
	if (mysql_stmt_prepare(stmt, INSERT_Status, strlen(INSERT_Status)))
	{
	  fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
	  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
	  exit(0);
	}
	fprintf(stdout, " prepare, INSERT successful\n");

	/* Get the parameter count from the statement */
	param_count= mysql_stmt_param_count(stmt);
	fprintf(stdout, " total parameters in INSERT: %d\n", param_count);

	memset(bind, 0, sizeof(bind));
	
	bind[0].buffer_type= MYSQL_TYPE_LONG;
	bind[0].buffer= (char *)&int_data;
	bind[0].is_null= 0;
	bind[0].length= 0;

	bind[1].buffer_type= MYSQL_TYPE_LONG;
	bind[1].buffer= (char *)&int_data2;
	bind[1].is_null= 0;
	bind[1].length= 0;

	/* Bind the buffers */
	if (mysql_stmt_bind_param(stmt, bind))
	{
	  fprintf(stderr, " mysql_stmt_bind_param() failed\n");
	  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
	  exit(0);
	}


	CreateUdpSock(&nSock, 6800);/* create UDP */
	while ((nSize=recvfrom(nSock, szBuf,256,0,paddr,&len)) > 0)/* receiveUDP datapackage */
	{
		//int *ID;
//		memcpy(ID,szBuf+2,4);
		//ID=szBuf+2;
		printf("@@@@@\n");
		int uid;
		uid=szBuf[2]*256*256*256+szBuf[3]*256*256+szBuf[4]*256+szBuf[5];
		printf("%d###",uid);

	/* Specify the data values for the first row */
	int_data= uid;             /* integer */


		if(szBuf[15]==0) /* recv client reset signal */
		{
			printf("%dBytes rec\n",nSize);
			szBuf[15]=1;
			szBuf[16]=255;
			SendMsgByUdp(szBuf,nSize,inet_ntoa(cliaddr.sin_addr), 6800);
			int_data2=0;
			if (mysql_stmt_execute(stmt))
			{
			  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
			  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
			  exit(0);
			}
		}
		else
		{
			printf("%c\n",szBuf[15]);
			/* recv client change status signal */
			if(szBuf[15]==5){
				szBuf[15]=6;
				szBuf[16]=0;
				SendMsgByUdp(szBuf,nSize,inet_ntoa(cliaddr.sin_addr), 6800);
				printf("recv client change status!\n");
				int_data2=1;
				if (mysql_stmt_execute(stmt))
				{
				  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
				  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
				  exit(0);
				}
			}
			else 
				/* emergance alarm */
				if(szBuf[15]==2){
				szBuf[15]=3;
				szBuf[16]=0;
				SendMsgByUdp(szBuf,nSize,inet_ntoa(cliaddr.sin_addr), 6800);
				printf("Client Alarm!\n");

				//save to db
				if((szBuf[16] & 0x01)>0)
				{
					printf("manual alarm!\n");
					int_data2=2;
				}
				else
				{
					if((szBuf[16] & 0x02)>0)
					{
						printf("door alarm!\n");
						int_data2=6;
					}
					else
					{
						printf("windows alarm!\n");
						int_data2=8;
					}
				}
				int_data2=2;
				if (mysql_stmt_execute(stmt))
				{
				  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
				  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
				  exit(0);
				}
				char sql_select[100];
				memset(sql_select,0,100);
				sprintf(sql_select,"select mobile from tblUser where devID=%d limit 1",uid);
				printf("%s\n",sql_select);
				mysql_real_query(conn2,sql_select,strlen(sql_select));
				result = mysql_store_result(conn2);
				printf("$$$$$$$$$$$$$$$$\n");

				
				row = mysql_fetch_row(result);
				printf("%s\n",row[0]);
				char *a=row[0];
				char sms[100];
				sprintf(sms,"php /home/ether/LAMPwork/udp/demo.php %.12s %d",a,uid);
//				sprintf(sms,"/home/ether/php5/bin/php /home/ether/LAMPwork/udp/demo.php %.12s %d",a,uid);
//				printf("%s\n",sms);

				system(sms);

				mysql_free_result(result);
			}
			printf("not reset!\n");
			int_data2=3;
			if (mysql_stmt_execute(stmt))
			{
			  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
			  fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
			  exit(0);
			}
		}
	}
	close(nSock);/* close udp  */
}
